<template>
  <div>
    <div id="searchForm">
      <el-form :inline="true" :model="searchForm" class="demo-form-inline" ref="searchForm">
        <el-form-item label="" prop="class_name">
          <el-input v-model="searchForm.class_name" placeholder="班级"></el-input>
        </el-form-item>
        <el-form-item label="" prop="student_name">
          <el-input v-model="searchForm.student_name" placeholder="学生姓名"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" plain @click="searchStudent">查询</el-button>
          <el-button type="primary" plain @click="exportExcel">导出数据</el-button>
          <el-button type="primary" plain @click="resetSearchFormData('searchForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-button type="primary" plain size="small" @click="add">新增</el-button>
    <el-button type="primary" plain size="small" @click="importExcel">批量导入</el-button>
    <el-table
      :data="tableData"
      style="width: 100%">
      <el-table-column
        prop="id"
        label="ID">
      </el-table-column>
      <el-table-column
        prop="student_no"
        label="学号">
      </el-table-column>
      <el-table-column
        prop="student_name"
        label="姓名">
      </el-table-column>
      <el-table-column
        prop="student_age"
        label="年龄">
      </el-table-column>
      <el-table-column
        prop="student_sex"
        label="性别">
      </el-table-column>
      <el-table-column
        prop="student_tel"
        width="130"
        label="联系方式">
      </el-table-column>
      <el-table-column
        prop="class_name"
        label="班级">
      </el-table-column>
      <el-table-column
        prop="course_name"
        label="课程">
      </el-table-column>
      <el-table-column
        prop="school_time"
        label="入学时间">
      </el-table-column>
      <el-table-column
        prop="student_state"
        width="180"
        label="状态">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.student_state"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-text="在学"
            inactive-text="休学"
            @change="changeState(scope.row.id,scope.row.student_state)"
            :active-value="1"
            :inactive-value="2">
          </el-switch>
        </template>


      </el-table-column>
      <el-table-column label="操作" width="180">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.row)">编辑
          </el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="pageSizes"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    <el-dialog :title="title" :visible.sync="dialogFormVisible" @close="clearInfo">
      <el-form :model="form" :rules="rules" ref="dialogForm">
        <el-form-item label="学号" :label-width="formLabelWidth" prop="student_no">
          <el-input v-model="form.student_no" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="姓名" :label-width="formLabelWidth" prop="student_name">
          <el-input v-model="form.student_name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="年龄" :label-width="formLabelWidth" prop="student_age">
          <el-input v-model="form.student_age" type="number" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="性别" :label-width="formLabelWidth" prop="student_gender">
          <el-select v-model="form.student_gender" placeholder="请选择性别" style="width: 100%">
            <el-option label="男" :value="1"></el-option>
            <el-option label="女" :value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="联系方式" :label-width="formLabelWidth" prop="student_tel">
          <el-input v-model="form.student_tel" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="班级" :label-width="formLabelWidth" prop="class_name">
          <el-select v-model="form.clazz_id" placeholder="请选择班级" style="width: 100%">
            <el-option :label="clazz.class_name" :value="clazz.id" v-for="clazz in classList"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="课程" :label-width="formLabelWidth" prop="course_name">
          <el-select v-model="form.course_id" placeholder="请选择课程" style="width: 100%">
            <el-option :label="course.course_name" :value="course.id" v-for="course in courseList"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="入学时间" :label-width="formLabelWidth" prop="school_time">
          <el-date-picker style="width: 100%"
                          v-model="form.school_time"
                          type="date"
                          format='yyyy-MM-dd'
                          value-format="yyyy-MM-dd"
                          placeholder="选择开班日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth" prop="student_state">
          <el-select v-model="form.student_state" placeholder="请选择状态" style="width: 100%">
            <el-option label="在学" :value="1"></el-option>
            <el-option label="休学" :value="2"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addOrUpDate">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="批量导入" :visible.sync="uploadDialogFormVisible" width="30%" @close="clearUploadInfo">
      <el-upload
        class="upload-demo"
        drag
        ref="upload"
        :on-success="importSuccess"
        :on-error="importError"
        action="http://localhost:8000/students/import/excel">
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
      </el-upload>
    </el-dialog>
  </div>
</template>
<script>

import {changStudentState, student_insert_edit, studentDelete, studentOne, studentPage} from "@/api/student";
import {courseList} from "@/api/course";
import {clazzList} from "@/api/clazz";
import {saveAs} from 'file-saver'

export default {
  name: 'index',
  data() {
    return {
      title: '新增',
      tableData: [],
      multipleSelection: [],
      uploadDialogFormVisible: false,
      courseList: [],
      classList: [],
      currentPage: 1,
      pageSizes: [5, 10, 15, 20],
      pageSize: 5,
      total: 0,
      rules: {
        student_name: [
          {required: true, message: '请输入学生姓名', trigger: 'blur'},
        ],
        student_no: [
          {required: true, message: '请输入学号', trigger: 'blur'}
        ],
        student_age: [
          {required: true, message: '请输入年龄', trigger: 'blur'}
        ],
        student_gender: [
          {required: true, message: '请选择性别', trigger: 'blur'}
        ],
        student_tel: [
          {required: true, message: '请输入联系方式', trigger: 'blur'}
        ],
        student_state: [
          {required: true, message: '请选择状态', trigger: 'blur'}
        ],
        class_name: [
          {required: true, message: '请选择班级', trigger: 'blur'}
        ],
        course_name: [
          {required: true, message: '请选择课程', trigger: 'blur'}
        ],
        school_time: [
          {required: true, message: '请选择入学时间', trigger: 'blur'}
        ],
      },
      formLabelWidth: '80px',
      dialogFormVisible: false,  //  控制对话框的显示隐藏
      form: {
        student_no: '',
        student_name: '',
        student_age: '',
        student_state: '',
        student_gender: '',
        student_tel: '',
        clazz_id: '',
        course_id: '',
        school_time: ''
      },
      searchForm: {
        class_name: '',
        student_name: ''
      }
    }
  },
  methods: {
    // 批量导出
    async exportExcel() {
      const response = await fetch(
        'http://localhost:8000/students/export/excel?class_name=' + this.searchForm.class_name + '&student_name' + this.searchForm.student_name
      )
      // 获取响应对象中的二进制数据
      const blob = await response.blob()
      // 保存二进制文件
      saveAs(blob, 'students.xlsx')
    },
    // 批量导入
    importExcel() {
      this.uploadDialogFormVisible = true
    },
    importSuccess(response) {
      if (response.code == 20000) {
        this.$notify.success(response.message)
        this.studentPageData()
        this.uploadDialogFormVisible = false
      } else {
        this.$notify.error(response.message)
      }
    },
    importError() {
      this.$notify.error('上传失败,请重试')
    },
    changeState(id, state) {
      changStudentState({id, state}).then(response => {
        this.$notify.success(response.message)
        this.studentPageData()
      })
    },
    resetSearchFormData(formName) {
      this.$refs[formName].resetFields()
      this.currentPage = 1
      this.studentPageData()
    },
    searchStudent() {
      this.currentPage = 1
      this.studentPageData()
    },
    // 编辑
    handleEdit(row) {
      this.title = '编辑'
      this.dialogFormVisible = true
      courseList().then(response => {
        this.courseList = response.data.courseList
      })
      clazzList().then(response => {
        this.classList = response.data.clazzList
      })
      studentOne(row.id).then(response => {
        this.form = response.data.student
      })
    },
    // 删除   合并到批量删除中
    handleDelete(row) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {   // 确定
        let {id} = row
        // 发送异步请求
        studentDelete(row.id).then(response => {
          this.$notify.success(response.message)
          this.studentPageData()
        })
      })
    },
    // 新增
    add() {
      this.title = '新增'
      this.dialogFormVisible = true
      //发送请求获取下拉选数据
      courseList().then(response => {
        this.courseList = response.data.courseList
      })
      clazzList().then(response => {
        this.classList = response.data.clazzList
      })
    },
    addOrUpDate() {
      student_insert_edit(this.form).then(response => {
        this.$notify.success(response.message)
        this.studentPageData()
        this.dialogFormVisible = false
      })
    },
    handleSizeChange(val) {
      this.pageSize = val
      // 重新获取分页数据
      this.currentPage = 1
      this.studentPageData()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.studentPageData()
    },
    //关闭弹窗时 清除表单数据
    clearInfo() {
      this.form = {
        student_no: '',
        student_name: '',
        student_age: '',
        student_state: '',
        student_gender: '',
        student_tel: '',
        clazz_id: '',
        course_id: '',
        school_time: ''
      }
      this.$refs.dialogForm.clearValidate()
    },
    clearUploadInfo() {
      this.$refs.upload.clearFiles()
    },
    studentPageData() {
      studentPage({
        page: this.currentPage,
        size: this.pageSize,
        class_name: this.searchForm.class_name,
        student_name: this.searchForm.student_name
      }).then(response => {
        this.total = response.data.total
        this.tableData = response.data.tableData
      })
    }
  },
  created() {
    this.studentPageData()
  }
}
</script>
<style scoped lang="scss">

</style>
